package com.uzai.console.service.cms.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.BaseDto;
import com.uzai.console.dto.cms.homepage.CmsHomePageConfigDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.cms.CmsHomePageService;
import com.uzai.console.service.feign.UzaiCmsFeignService;
import com.uzai.console.vo.cms.homepage.CmsHomePageConfigVo;
import com.uzai.console.vo.feign.uzaicms.CMSBanner;
import com.uzai.console.vo.feign.uzaicms.HomeActNavEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class CmsHomePageServiceImpl implements CmsHomePageService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private UzaiCmsFeignService uzaiCmsFeignService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询CMS首页设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public CmsHomePageConfigVo findCmsHomePageInfo(Long merId){
        CmsHomePageConfigVo cmsHomePageConfigVo = new CmsHomePageConfigVo();
        //查询运营商CMS首页设置
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.CMS_HOMEPAGE_CONFIG.getValue());
        if(merBaseConfig != null ){
            cmsHomePageConfigVo.setCmsHomePageConfig(merBaseConfig.getVal());
        }
        return cmsHomePageConfigVo;
    }


    /**
     * 保存CMS首页设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param cmsHomePageConfigDto
     * @return null
     */
    public void updateCmsHomePage(CmsHomePageConfigDto cmsHomePageConfigDto){

        if(StringUtils.isBlank(cmsHomePageConfigDto.getCmsHomepageConfig())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入CMS首页配置内容");
        }

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(cmsHomePageConfigDto.getMerId(), MerConfigKeyEnum.CMS_HOMEPAGE_CONFIG.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(cmsHomePageConfigDto.getCmsHomepageConfig());
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(cmsHomePageConfigDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.CMS_HOMEPAGE_CONFIG.getValue());
            merBaseConfig.setVal(cmsHomePageConfigDto.getCmsHomepageConfig());
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //更新redis
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.CMS_HOMEPAGE_CONFIG.getKey(), cmsHomePageConfigDto.getMerId()), merBaseConfig.getVal());


    }

    /**
     * 查询首页轮播图片列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsTbGoodsInfoVo
     */
    public List<CMSBanner> findHomeActCarouseList(){

        //新建返回到页面的LIST对象
        List<CMSBanner> cmsBannerList = new ArrayList<>();

        UzaiRespVo<List<CMSBanner>> uzaiRespVo =  uzaiCmsFeignService.homeActCarouseList(new BaseDto());
        if(uzaiRespVo.getCode() == 200){
            if(uzaiRespVo.getData() != null){
                cmsBannerList = uzaiRespVo.getData();
            }
        }
        //将分页信息复制到返回到页面的分页MO当中
        return cmsBannerList;
    }

    /**
     * 查询首页优惠头条列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsTbGoodsInfoVo
     */
    public List<CMSBanner> findHomeActDiscountHeadlineList(){

        //新建返回到页面的LIST对象
        List<CMSBanner> cmsBannerList = new ArrayList<>();

        UzaiRespVo<List<CMSBanner>> uzaiRespVo =  uzaiCmsFeignService.homeActDiscountHeadline(new BaseDto());
        if(uzaiRespVo.getCode() == 200){
            if(uzaiRespVo.getData() != null){
                cmsBannerList = uzaiRespVo.getData();
            }
        }
        //将分页信息复制到返回到页面的分页MO当中
        return cmsBannerList;
    }


    /**
     * 查询首页活动配置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsTbGoodsInfoVo
     */
    public List<HomeActNavEntity> findHomeActNavList(){

        //新建返回到页面的LIST对象
        List<HomeActNavEntity> homeActNavEntityList = new ArrayList<>();

        UzaiRespVo<List<HomeActNavEntity>> uzaiRespVo =  uzaiCmsFeignService.homeActNavList(new BaseDto());
        if(uzaiRespVo.getCode() == 200){
            if(uzaiRespVo.getData() != null){
                homeActNavEntityList = uzaiRespVo.getData();
                //排除未启用的活动
                Iterator<HomeActNavEntity> iterator = homeActNavEntityList.iterator();
                while (iterator.hasNext()){
                    HomeActNavEntity next = iterator.next();
                    if(next.getAvailable()!=null&&next.getAvailable()==0){
                        iterator.remove();
                    }
                }
            }
        }
        //将分页信息复制到返回到页面的分页MO当中
        return homeActNavEntityList;
    }

}
